//
//  HttpUtil.m
//  HttpDemo
//  Http帮助类
//  Created by 李 亚春 on 12-9-9.
//  Copyright (c) 2012年 epoint. All rights reserved.
//

#import "HttpUtil.h"
#import "ASIHTTPRequest.h"
#import "ASIDataDecompressor.h"
#import "ASIFormDataRequest.h"
#import "StringUtil.h"

@implementation HttpUtil


+(NSString *)getHttpBackStringByUrlWithZip:(NSString *)url
{
    NSLog(@"请求URL%@",url);
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setTimeoutInterval:10];
    [request setURL:[NSURL URLWithString:url]];
    
    [request setHTTPMethod:@"GET"];
    
    NSHTTPURLResponse* urlResponse = nil;
    
    NSError *error = [[NSError alloc] init];
    
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    
    NSData *decompressData = [ASIDataDecompressor uncompressData:responseData error:nil];
    
    NSString *result = [[NSString alloc] initWithData:decompressData encoding:NSUTF8StringEncoding];
    
    //获取返回的内容
    
    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
    {
        return result;
    }
    return @"Time Connect Out";
}

+(NSString *)getHttpBackStringByUrlWithNoZip:(NSString *)url
{
    NSLog(@"请求URL%@",url);
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setTimeoutInterval:10];
    [request setURL:[NSURL URLWithString:url]];
    
    [request setHTTPMethod:@"GET"];
    
    NSHTTPURLResponse* urlResponse = nil;
    
    NSError *error = [[NSError alloc] init];
    
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    
    //获取返回的内容
    
    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
    {
        return result;
    }
    return @"Time Connect Out";
}

+(NSString *)getHttpBackString:(NSString *)url
{
    ASIHTTPRequest *asRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [asRequest startSynchronous];
    NSData *responseData = [asRequest responseData];
    NSString *bs = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    return bs;
}

+(NSData *)getHttpData:(NSString *)url
{
    ASIHTTPRequest *asRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [asRequest startSynchronous];
    NSData *responseData = [asRequest responseData];
    return responseData;
}

+(void)post:(ASIHTTPRequest *)_request url:(NSString *)_url textParams:(NSMutableDictionary *)_textParams fileParams:(NSMutableDictionary *)_fileParams
{
    NSString *BOUNDARY = [StringUtil gen_uuid];
    NSString *PREFIX = @"--", *LINEND = @"\r\n";
    NSString *MULTIPART_FROM_DATA = @"multipart/form-data";
    NSString *CHARSET = @"UTF-8";
    
    [_request setRequestMethod:@"post"];
    [_request addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"%@;boundary=%@",MULTIPART_FROM_DATA,BOUNDARY]];
    
    //拼接文本数据
    NSMutableString *sb = [[NSMutableString alloc]init];
    for (NSString *key in _textParams)
    {
        [sb appendString:PREFIX];
        [sb appendString:BOUNDARY];
        [sb appendString:LINEND];
        [sb appendString:@"Content-Disposition: form-data; name=\""];
        [sb appendString:key];
        [sb appendString:@"\""];
        [sb appendString:LINEND];
        [sb appendString:@"Content-Type: text/plain; charset="];
        [sb appendString:CHARSET];
        [sb appendString:LINEND];
        [sb appendString:@"Content-Transfer-Encoding: 8bit"];
        [sb appendString:LINEND];
        [sb appendString:LINEND];
        [sb appendString:[_textParams objectForKey:key]];
        [sb appendString:LINEND];
    }
    [_request appendPostData:[sb dataUsingEncoding:NSUTF8StringEncoding]];
    
    for (NSString *key in _fileParams)
    {
        NSMutableString *sb = [[NSMutableString alloc]init];
        [sb appendString:PREFIX];
        [sb appendString:BOUNDARY];
        [sb appendString:LINEND];
        [sb appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\""];
        [sb appendString:key];
        [sb appendString:@"\""];
        [sb appendString:LINEND];
        [sb appendString:@"Content-Type: application/octet-stream; charset="];
        [sb appendString:CHARSET];
        [sb appendString:LINEND];
        [sb appendString:LINEND];
        [_request appendPostData:[sb dataUsingEncoding:NSUTF8StringEncoding]];
        [_request appendPostDataFromFile:[_fileParams objectForKey:key]];
        [_request appendPostData:[LINEND dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    // 拼接结束标志
    NSMutableString *endString = [[NSMutableString alloc]init];
    [endString appendString:PREFIX];
    [endString appendString:BOUNDARY];
    [endString appendString:PREFIX];
    [endString appendString:LINEND];
    
    [_request appendPostData:[endString dataUsingEncoding:NSUTF8StringEncoding]];
}

@end
